package com.gift.search;

import cn.sangyy.gift.SearchStart;
import cn.sangyy.gift.doc.SpuGiftDoc;
import cn.sangyy.gift.repository.SpuGiftRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author SangYY
 * @date: 2023/04/06/19:11
 */
@SpringBootTest(classes = SearchStart.class)
public class test {

    @Autowired
    private ElasticsearchRestTemplate template;
    @Autowired
    private SpuGiftRepository repository;

    @Test
    public void preIndex(){
        //创建gift索引和mapping
        template.createIndex(SpuGiftDoc.class);
        template.putMapping(SpuGiftDoc.class);
    }

    @Test
    public void testGiftBatchAdd() throws Exception{
        List<SpuGiftDoc> giftDocs = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            SpuGiftDoc doc = new SpuGiftDoc();
            doc.setId(i+1L);
            doc.setSpuId(1L);
            doc.setCover("https://img11.360buyimg.com/n7/jfs/t1/197008/29/28074/30682/63244a99E25c50323/a729014d8c47ac3b.jpg.avif");
            doc.setName("荣耀X40 120Hz OLED硬核曲屏 5100mAh 快充大电池 7.9mm轻薄设计 5G手机"+i);
            doc.setTenantId(1L);
            doc.setTenantName("姚桑官方旗舰店");
            if (i%2==0){
                doc.setBrandId(1L);
                doc.setBrandName("荣耀手机");
                doc.setTypeId(1L);
                doc.setTypeName("4G手机");
                doc.setCommentCount(20);
                doc.setSaleCount(90);
                doc.setMaxPrice(new BigDecimal(3000));
                doc.setMinPrice(new BigDecimal(2000));
            }else if(i%3 == 0){
                doc.setBrandId(2L);
                doc.setBrandName("华为手机");
                doc.setTypeId(2L);
                doc.setTypeName("5G手机");
                doc.setCommentCount(10);
                doc.setSaleCount(100);
                doc.setMaxPrice(new BigDecimal(5000));
                doc.setMinPrice(new BigDecimal(2000));
            }else {
                doc.setBrandId(3L);
                doc.setBrandName("小米手机");
                doc.setTypeId(3L);
                doc.setTypeName("5G手机");
                doc.setCommentCount(15);
                doc.setSaleCount(80);
                doc.setMaxPrice(new BigDecimal(4000));
                doc.setMinPrice(new BigDecimal(2000));
            }
            doc.setOnSaleTime(new Date());
            giftDocs.add(doc);
        }
        repository.saveAll(giftDocs);
    }
}
